package com.maybach.web.controller;

import com.audi.dubbo.api.admin.models.AdminModel;
import com.maybach.common.BizResult;
import com.maybach.common.enums.BaseConstants;
import com.maybach.common.model.CommonEnumDO;
import com.maybach.common.query.CommonEnumQuery;
import com.maybach.component.city.CityData;
import com.maybach.component.city.domain.City;
import com.maybach.component.city.domain.Province;
import com.maybach.core.dubbo.consumer.adapter.AdminServiceDubooAdapter;
import com.maybach.core.manager.CommonEnumManager;
import com.maybach.core.manager.ImageManager;
import com.maybach.core.manager.impl.ImageManagerImpl;
import com.maybach.util.StringUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/common/")
public class CommonController {
    @Autowired
    protected ImageManager imageManager;
    @Autowired
    protected CommonEnumManager commonEnumManager;
    @Autowired
    AdminServiceDubooAdapter adminServiceDubooAdapter;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 上传图片
     *
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "uploadImage.json")
    public Map<String, String> uploadImage(@RequestParam(required = false, value = "file") CommonsMultipartFile multipartFile, ModelMap model) {
        FileItem fileItem = multipartFile.getFileItem();
        if (fileItem != null) {
            String url = null;
            try {
                url = imageManager.uploadFile(fileItem);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (StringUtils.isBlank(url)) {
                Map m = new HashMap();
                m.put("error", 1);
                m.put("code", 1002);
                m.put("msg", "上传图片失败");
                return m;
            }
            Map m = ImageManagerImpl.getSize(url);
            m.put("error", 0);
            m.put("code", 1001);
            m.put("url", url);
            return m;
        } else {
            Map m = new HashMap();
            m.put("error", 1);
            m.put("code", 1002);
            m.put("msg", "图片格式出错或过大");
            return m;
        }
    }

    /**
     * 获取单个省份的全部地区
     * @param proId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getCitys.json")
    public List<City> getCitys(@RequestParam(value = "proId") Integer proId) {
        List<City> citys = CityData.getCity(proId);
        if (citys == null) {
            return Collections.EMPTY_LIST;
        }
        return citys;
    }

    /**
     * 获取全部的省份
     * @param model
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAllPros.json")
    public List<Province> getAllPros(ModelMap model) {
        List<Province> pros = CityData.getProvince();
        if (pros == null) {
            return Collections.EMPTY_LIST;
        }
        return pros;
    }

    /**
     * 获取全部的省份
     * @param
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getAdminByRole.json")
    public BizResult getAdminByRole(@RequestParam(value = "role") String role){

        BizResult result = new BizResult();
        if(StringUtils.isBlank(role)){
            result.setResultCodeAndMsg(BaseConstants.ResultCode.PARAM_ERROR);
        }else{
            result.setResultDate(this.getUsersByDubbo(role));
        }

        return result;

    }


    private Map<String,String> getUsers(String role){
        CommonEnumQuery query = new CommonEnumQuery();
        CommonEnumQuery.Criteria criteria  = query.or();
        criteria.andGroupCodeEqualTo(role);
        criteria.andStatusEqualTo(0);
        query.setOrderByClause(" sort_order ");
        List<CommonEnumDO> enumDOs = this.commonEnumManager.selectByQuery(query);
        Map<String, String> map = new HashMap<String, String>();
        for(CommonEnumDO commonEnumDO : enumDOs){
            map.put(commonEnumDO.getCode(), commonEnumDO.getName());
        }
        return map;
    }


    private Map<String,String> getUsersByDubbo(String role){
        List<AdminModel> list = adminServiceDubooAdapter.getAdminModelByRoleCode(role);

        Map<String, String> map = new HashMap<String, String>();
        for(AdminModel model : list){
            map.put(model.getAdminId()+"", getName(model));
        }
        return map;
    }

    private String getName(AdminModel model){
        if(StringUtil.isNotBlank(model.getExtattr())){
            return model.getExtattr();
        }
        return model.getNick();
    }





}